﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>iReader后台</title>
  <link href="stylesheets/reset.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="scripts/browser-compatibility.js"></script>
  <link href="stylesheets/layout-css-1.css" rel="stylesheet" type="text/css" />
  <link href="stylesheets/faary.css" rel="stylesheet" />
  <script type="text/javascript" src="scripts/jquery.js"></script>
  <script src="uploadify/swfobject.js"></script>
  <script src="scripts/jquery.Block.js"></script>
  <script type="text/javascript" src="uploadify/jquery.uploadify.js"></script>
  <script type="text/javascript" src="uploadify/jquery.uploadify.min.js"></script>
  <script type="text/javascript">
      //load Category
      function loadCategory() {
          var categoryBlock = $('#categoryselect');
          categoryBlock.html("");
          var html = '';
          html += '<option value=-1>选择类别</option>';
          var postData = "{'q_tit':" + null + "," +
                          "'startIndex':" + null + "," +
                          "'length':" + null + "," +
                          "'startTime':" + null + "," +
                          "'endTime':" + null + "," +
                          "'orderJsonObject':" + null + "}";
          $.ajax({
              url: './iReaderWebSrvs.asmx/QueryCategory',
              contentType: "application/json;charset=utf-8",
              type: 'post',
              data: postData,
              dataType: 'json',
              success: function (json) {
                  var r = $.parseJSON(json.d);
                  if (r.IsProcessSuccess) {
                      var response = $.parseJSON(r.Response);
                      for (var i = 0; i < response.length; i++) {
                          html += "<option value=" + response[i].ID + ">" + response[i].Title + "</option>";
                      }
                      html += "<option value=-2 style=\"color:#bebebe\">添加类别</option>";
                      categoryBlock.append(html);
                  }
                  else {
                      html += "<option value=-2 style=\"color:#bebebe\">添加类别</option>";
                      categoryBlock.append(html);
                  }
              },
              error: function (er) {
                  html += "<option value=-2 style=\"color:#bebebe\">添加类别</option>";
                  categoryBlock.append(html);
              }
          });
      }

      function thumbSelected() {
          return $('#thumbselected').val() == "true";
      }

      function sourceSelected() {
          return $('#sourceselected').val() == "true";
      }

      function onSubmit() {
          //Analysis
          var titleBlock = $('#bkTitle');
          var cateBlock = $('#categoryselect');
          var descBlock = $('#bkDesc');
          var albumBlock = $('#addalbumselect');
          var thumbBlock = $('#thumbfile');
          var sourceBlock = $('#sourcefile');
          if (titleBlock.val() == "") {
              alert('书籍抬头不能为空');
          }
          else if (cateBlock.val() == -1) {
              alert('书籍类别不能为空');
          }
          else if (!sourceSelected()) {
              alert('请选择书籍档案');
          }
          else {
              $('#Content').block({
                  message: '<h1>处理中...</h1>',
                  css: { border: '3px solid #a00' }
              });
              $('#sourcefile').uploadify('upload', '*');
          }
      }

      function addCategorySubmit() {
          var categoryTitleBlock = $('#categoryTitle');
          var categoryDescBlock = $('#categoryDesc');
          if (categoryTitleBlock.val() == "")
              alert('请输入类别名');
          else {
              var postData = "{'tit':'" + categoryTitleBlock.val() + "'," +
                              "'desc':'" + categoryDescBlock.val() + "'}";
              $.ajax({
                  url: 'iReaderWebSrvs.asmx/AddCategroy',
                  contentType: "application/json;charset=utf-8",
                  type: 'post',
                  data: postData,
                  dataType: 'json',
                  success: function (json) {
                      var response = $.parseJSON(json.d);
                      if (response.IsProcessSuccess) {
                          alert("添加成功");
                          loadCategory();
                          cancelCategorySubmit();
                      }
                  },
                  error: function (er) {
                      alert(er.statusText);
                  }
              });
          }
      }

      function cancelCategorySubmit() {
          document.getElementById("categoryselect").selectedIndex = 0;
          $.unblockUI();
      }

      function clearField() {
          var titleBlock = $('#albumTitle');
          var cateBlock = $('#categoryselect');
          var descBlock = $('#albumDesc');
          var thumbBlock = $('#thumbfile');
          var sourceBlock = $('#sourcefile');

          titleBlock.val("");
          cateBlock.val("0");
          albumBlock.val("0");
          descBlock.val("");
          $('#sourceselected').val("false");
          $('#thumbselected').val("false");
      }

      $(document).ready(function () {
          loadCategory();

          var thumbselectedBlock = $('#thumbselected');
          var sourceselectedBlock = $('#sourceselected');

          $('#thumbfile').uploadify({
              'width': 329,
              'queueID': 'uploaderQueue',
              'formData': { 'uploadType': 'thumb', 'fname': 'empty','ext':'empty' },
              'swf': 'uploadify/uploadify.swf',
              'uploader': 'UploadHandler.ashx',
              'cancelImg': 'uploadify/uploadify-cancel.png',
              'fileTypeDesc': '选择图片文件',
              'fileTypeExts': '*.bmp; *.jpg;*.jpeg;*.png;*.gif',
              'fileSizeLimit': 2048,
              'buttonText': '选择封面',
              'buttonClass': 'heading2',
              'auto': false,
              'multi': false,
              'onFallback': function () {
                  alert("您未安装FLASH控件，无法上传图片！请安装FLASH控件后再试。");
              },
              'onSelect': function (file) {
                  thumbselectedBlock.val("true");
                  var ext = file.name.split(".")[1];
                  $('#thumbext').val(ext);
              },
              'onCancel': function () {
                  thumbselectedBlock.val("false");
              },
              'onSelectError': function (file, errorCode, errorMsg) {
                  thumbselectedBlock.val("false");
                  switch (errorCode) {
                      case -100:
                          alert("上传的文件数量已经超出系统限制的" + $('#thumbfile').uploadify('settings', 'queueSizeLimit') + "个文件！");
                          break;
                      case -110:
                          alert("文件 [" + file.name + "] 大小超出系统限制的" + $('#thumbfile').uploadify('settings', 'fileSizeLimit') + "大小！");
                          break;
                      case -120:
                          alert("文件 [" + file.name + "] 大小异常！");
                          break;
                      case -130:
                          alert("文件 [" + file.name + "] 类型不正确！");
                          break;
                  }
              },
              'onUploadStart': function (file) {
                  $('#Content').block({
                      message: '<h1>上传图书封面...</h1>',
                      css: { border: '3px solid #a00' }
                  });

                  var filenames = $('#sFilenames');

                  var thumbfile = filenames.val().split(";")[0];
                  if (thumbfile == "")
                      $('#thumbfile').uploadify('cancel', '*');
                  else {
                      $('#thumbfile').uploadify("settings", "formData", { 'uploadType': 'thumb', 'fname': thumbfile, 'ext':$('#thumbext').val() });
                  }
              },
              'onUploadSuccess': function (file) {
                  $('#Content').block({
                      message: '<h1>封面上传完毕</h1>',
                      css: { border: '3px solid #a00' }
                  });
                  setTimeout($('#Content').unblock(), 2000);
                  clearField();
              }
          });

          $('#sourcefile').uploadify({
              'width': 329,
              'queueID': 'uploaderQueue',
              'formData': { 'uploadType': 'source' },
              'swf': 'uploadify/uploadify.swf',
              'uploader': 'UploadHandler.ashx',
              'cancelImg': 'uploadify/uploadify-cancel.png',
              'fileTypeDesc': '选择图书文档',
              'fileTypeExts': '*.pdf;*.doc;*.epub;*.docx;*.txt',
              'folder': '/uploads',
              'buttonText': '添加书籍档案',
              'buttonClass': 'heading1',
              'auto': false,
              'multi': true,
              'onFallback': function () {
                  alert("您未安装FLASH控件，无法上传图片！请安装FLASH控件后再试。");
              },
              'onSelect': function (file) {
                  sourceselectedBlock.val("true");
              },
              'onCancel': function () {
                  sourceselectedBlock.val("false");
              },
              'onSelectError': function (file, errorCode, errorMsg, errorString) {
                  sourceselectedBlock.val("false");
                  switch (errorCode) {
                      case -100:
                          alert("上传的文件数量已经超出系统限制的" + $('#thumbfile').uploadify('settings', 'queueSizeLimit') + "个文件！");
                          break;
                      case -110:
                          alert("文件 [" + file.name + "] 大小超出系统限制的" + $('#thumbfile').uploadify('settings', 'fileSizeLimit') + "大小！");
                          break;
                      case -120:
                          alert("文件 [" + file.name + "] 大小异常！");
                          break;
                      case -130:
                          alert("文件 [" + file.name + "] 类型不正确！");
                          break;
                      default:
                          alert(errorString);
                          break;
                  }
              },
              'onUploadSuccess': function (file, data, response) {
                  var filenames = $('#sFilenames');
                  var value = filenames.val();
                  value += data + ";";
                  filenames.val(value);
              },
              'onUploadError': function (file, errorCode, errorMsg, errorString) {
                  alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
                  $.unblockUI();
              },
              'onQueueComplete': function (queueData) {
                  
                  var titleBlock = $('#albumTitle');
                  var cateBlock = $('#categoryselect');
                  var descBlock = $('#albumDesc');
                  var filenames = $('#sFilenames');
                  var thumbfile = filenames.val().split(';')[0].split('.')[0] + "." + $('#thumbext').val();

                  var postData = "{'tit':'" + titleBlock.val() + "'," +
                                  "'desc':'" + descBlock.val() + "'," +
                                  "'cid':'" + cateBlock.val() + "'," +
                                  "'bookSrcs':'" + filenames.val() + "'," + 
                                  "'thumb':'" + thumbfile + "'}";
                  $.ajax({
                      url: 'iReaderWebSrvs.asmx/AddAlbum',
                      contentType: "application/json;charset=utf-8",
                      type: 'post',
                      data: postData,
                      dataType: 'json',
                      beforeSend: function () {
                          $('#Content').block({
                              message: '<h1>正在添加到数据库...</h1>',
                              css: { border: '3px solid #a00' }
                          });
                      },
                      success: function (json) {
                          var data = $.parseJSON(json.d);
                          if (thumbSelected() && data.IsProcessSuccess) {
                              $('#thumbfile').uploadify('upload', '*');
                          }

                          if (!data.IsProcessSuccess) {
                              alert('failed');
                          }
                      },
                      complete: function () {
                          clearField();
                          alert(queueData.uploadsSuccessful + ' files were successfully uploaded');
                      },
                      error: function (er) {
                          alert(er.statusText);
                      }
                  });
              }
          });

          $('#categoryselect').change(function () {
              switch ($(this).val()) {
                  case "-1":
                      break;
                  case "-2":
                      $.blockUI({ message: $('#addcategory'), css: { width: '320px' } });
                      break;

              }
          });
      });
  </script>
 </head>
 <body>
  <div class="main_wrapper cf" style="z-index:3">
   <input type="hidden" value="false" id="sourceselected"/>
   <input type="hidden" value="false" id="thumbselected"/>
   <input type="hidden" value="" id="sFilenames"/>
   <input type="hidden" value="empty" id="thumbext" />
   <div class="Header cf"></div>
   <div class="Content cf" id="Content">
    <div class="content_header cf">
     <div class="control_btn">	<a href="index.html"><img src="images/back.png" width="44px" height="44px"/></a>

     </div>
     <div class="heading">
      	<h3 class="heading">iReader 后台管理 - 添加图书系列</h3>

     </div>
    </div>
    <div class="row_6 cf"></div>
    <div class="content_form cf">
     <div class="book_detail">
      <div class="iform">
       <ul>
        <li>
         <label for="">*系列名称</label>
         <input class="itext" type="text" name="" id="albumTitle">
        </li>
        <li>
         <label for="">*书籍类别</label>
         <select class="iselect" name="" id="categoryselect"></select>
        </li>
        <li>
         <label for="">*描述</label>
         <textarea class="itextarea" name="" id="albumDesc"></textarea>
        </li>
       </ul>
      </div>
     </div>
     <div class="book_source cf">
      <div class="book_thumb cf">
        <h3 class="heading2" id="thumbfile">上传封面</h3>

      </div>
      <div class="book_src cf">
        <h3 class="heading1" id="sourcefile">添加书籍档案</h3>

      </div>
     </div>
    </div>
    <div class="row_5 cf"> <a href="javascript:onSubmit()" class="submit" target="_self">确认</a>

    </div>
   </div>
   <div class="Footer cf">
    <div class="uploaderQueue cf" id="uploaderQueue"></div>
   </div>

   <div id="addcategory" style="display:none;z-index:-1">
     <h1>添加类别</h1> 
    <p>
     <input type="text" placeholder="输入类别名称" id="categoryTitle" style="width:200px">
    </p>
    <p>
     <textarea placeholder="输入类别描述" id="categoryDesc" style="width:200px;min-height:100px"></textarea>
    </p>
    <p>
     <input type="button" id="addCategory_submit" value="添加" onclick="javascript: addCategorySubmit()">
     <input type="button" id="addCategory_cancel" value="取消" onclick="javascript: cancelCategorySubmit()">
    </p>
   </div>
  </div>
 </body>

</html>
